package cn.bocai.pc.web.controller;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.bocai.pc.model.BcAdmin;
import cn.bocai.pc.model.BcUser;
import cn.bocai.pc.service.BcAdminService;
import cn.bocai.pc.util.MD5Utils;

@Controller
@RequestMapping("/user")
public class UserManageController {

	@Autowired
	private BcAdminService  BaService;
	
	//后台用户登入
	@RequestMapping("/except/login.do")
	public String login(HttpServletRequest request, HttpServletResponse response,BcAdmin baLog,Model m){
		/* m.addAttribute("skipUrl","/login.jsp");
	 	 if("".equals(baLog.getUsername())||null==baLog.getUsername()||"".equals(baLog.getPassword())||null==baLog.getPassword()){
    		 m.addAttribute("msg","用户名或密码不能为空");
    	 }else{
    		 baLog.setPassword(MD5Utils.md5(baLog.getPassword()));
    		 BcAdmin baUserLogin = BaService.selectLogin(baLog);
        	 if(null==baUserLogin){
        		 m.addAttribute("msg","用户名或密码错误");
        	 }else{
        		 m.addAttribute("msg","登入成功，谢谢！");
        		 request.getSession().setAttribute("baUserLogin", baUserLogin);
        		 m.addAttribute("skipUrl","/skip/ifram.do");
        	 }
          }*/
	 	     m.addAttribute("skipUrl","/skip/ifram.do");//改行完成以后要删除
	 	 return "page/prompt/publicPrompt";
	}
	
	
	//安全退出
	@RequestMapping("/outLogin.do")
	public String outLogin(HttpServletRequest request,Model m){
		request.getSession().removeAttribute("baUserLogin");
		m.addAttribute("msg","已安全退出，谢谢！");
		m.addAttribute("skipUrl","/login.jsp");
		return "page/prompt/publicPrompt";
	}
	//修改密码
	@RequestMapping("/doModifyPwd.do")
	public String doModifyPwd(HttpServletRequest request, HttpServletResponse response,BcAdmin baLog,Model m){
		 String oldPaw =MD5Utils.md5(request.getParameter("password")) ;
    	 String newPaw =MD5Utils.md5(request.getParameter("newPwd"));
		BcAdmin baAttribute = (BcAdmin)request.getSession().getAttribute("baUserLogin");
    	 String password = baAttribute.getPassword();
    	 m.addAttribute("skipUrl","/skip/ifram.do");
    	 if(!password.equals(oldPaw)){
    		 m.addAttribute("msg","输入密码有误");
    	 }else{
    		 //TODO  修改密码
    		 HashMap<String, Object> map = new HashMap<String, Object>();
    		 map.put("userId", baAttribute.getUserId());
    		 map.put("password", newPaw);
    		 int updateUserPaw = BaService.updateUserPaw(map);
    		 if(!"0".equals(updateUserPaw)){
    			 request.getSession().removeAttribute("selectLogin");
    			 m.addAttribute("msg","密码修改成功");
    			 m.addAttribute("skipUrl","/login.jsp");
    		 }
    	 }
    	 return "page/prompt/publicPrompt";
	}
	
}
